//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 12
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace GetFileAttachmentAndSave
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            // TODO:  Need an attachment Id in order to call this.
            /*GetFileAttachmentAndSave(
                binding,
                @"c:\",
                myAttachmentId);*/
        }

        /// <summary>
        /// Gets the file attachments and saves them to a directory (Listing 12-6)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="destinationPath">Destination directory to save the 
        /// attachments to</param>
        /// <param name="attachmentIds">Array of attachment Ids to retrieve</param>
        ///
        public static void GetFileAttachmentAndSave(
                                   ExchangeServiceBinding binding,
                                   string destinationPath,
                                   params AttachmentIdType[] attachmentIds)
        {
            GetAttachmentType getAttachmentRequest = new GetAttachmentType();

            // Note that AttachmentIdType derives from RequestAttachmentIdType, so 
            // we can directly assign here.
            //
            getAttachmentRequest.AttachmentIds = attachmentIds;

            // Remember, we don't even need to set the attachment shape.
            //
            GetAttachmentResponseType getAttachmentResponse =
                            binding.GetAttachment(getAttachmentRequest);

            // Now, here we might have asked for multiple items.  If so, we will get 
            // back multiple response messages.

            foreach (AttachmentInfoResponseMessageType attachmentResponseMessage in
                        getAttachmentResponse.ResponseMessages.Items)
            {
                if (attachmentResponseMessage.ResponseCode ==
                                               ResponseCodeType.NoError)
                {
                    FileAttachmentType fileAttachment =
                          attachmentResponseMessage.Attachments[0] as FileAttachmentType;

                    // if fileAttachment is null, then it must be an item attachment 
                    // which we will not save out.
                    //
                    if (fileAttachment != null)
                    {
                        // Now, just save out the file contents
                        //
                        using (FileStream file = File.Create(
                                 Path.Combine(destinationPath, fileAttachment.Name)))
                        {
                            file.Write(
                                   fileAttachment.Content,
                                   0,
                                   fileAttachment.Content.Length);
                            file.Flush();
                            file.Close();
                        }
                    }
                }
            }
        }

    }
}
